/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.command;

import dom.command.Result;
import dom.command.Status;
import dom.command.UndoableCommand;
import imp.reciever.encryptor.Encryptor;

/**
 *
 * @author julian.pena
 */
public class Encrypt implements UndoableCommand{
    
    private Encryptor encryptor;
    private final CommandType commandType = CommandType.ENCRYPT;
    private Status status;
    private Result result;
    
    public Encrypt(Encryptor encryptor) {
        this.encryptor = encryptor;
        this.status = Status.NOT_STARTED;
    }

    //<editor-fold defaultstate="collapsed" desc="methods">
    @Override
    public void undo() {
        try {
            status = Status.IN_PROGRESS;
            encryptor.decode();
            status = Status.FINISHED;
            result = Result.SUCCESSFUL;
        } catch (Exception ex) {
            status = Status.FINISHED;
            result = Result.FAILED;
            System.out.println(ex.getMessage());
        }
    }
    
    @Override
    public void redo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void execute() {
        try {
            status = Status.IN_PROGRESS;
            encryptor.action();
            status = Status.FINISHED;
            result = Result.SUCCESSFUL;
        } catch (Exception ex) {
            status = Status.FINISHED;
            result = Result.FAILED;
            System.out.println(ex.getMessage());
        }
    }
    
    @Override
    public Status getStatus() {
        return status;
    }
    
    @Override
    public Result getResult() {
        return result;
    }
    
    @Override
    public String getName() {
        return commandType.getStringValue();
    }
    
    @Override
    public void run() {
        execute();
    }
    //</editor-fold>
    
}
